<?php
    include("include/header.inc.php");
    include('include/func.inc.php');
    $_SESSION['page'] = 'event';
?>
    <div data-role="content">
        <H2><?php echo $texts['event'][$_SESSION['lang']] ?></h2>
        <div class="tableauPage">

        
        <!-- form for dates and thematic -->

        <?php
            //check if values were sent with form
            $dateFrom = date('d.m.Y');
            $dateTo = date("d").'.'.date("m").'.'.(date("Y")+1);

            $thematic = $_POST['thematic'];
            if(isset($_POST)){
                if(isset($_POST['from'])){
                    $dateFrom = $_POST['from'];
                }

                if(isset($_POST['to'])){
                    $dateTo = $_POST['to'];
                }

                $thematicID = $_POST['thematic'];
            }
            $dfrom = explode(".", $dateFrom); 
            $dto = explode(".", $dateTo);

            $finabFrom = $dfrom[2].$dfrom[1].$dfrom[0];
            $finabTo = $dto[2].$dto[1].$dto[0]; 

            if($finabFrom > $finabTo){
                echo '<h3>' . $texts['dates_error'][$_SESSION['lang']] . '<h3/>';    
            }  

            /*$LOGIN = array ("ASHOST"=>"153.109.124.33",
                                "SYSNR"=>"D07",
                                "CLIENT"=>"100",
                                "USER"=>"M646-04",
                                "PASSWD"=>"hevs12",
                                "CODEPAGE"=>"1100");*/
        ?>
        <form method="post">
            <label for="from"><?php echo $texts['from'][$_SESSION['lang']] ?>:</label>
            <input type="text" id="dateFrom" name="from" value="<?php echo $dateFrom ?>" />

            <label for="to"><?php echo $texts['to'][$_SESSION['lang']] ?>:</label>
            <input type="text" id="dateTo" name="to" value="<?php echo $dateTo ?>"/>


            <?
                //Get all the information about the different thematics
                //Try to connect to SAP using our Login array
                /*$rfc = saprfc_open ($LOGIN);
                if(!$rfc){
                        echo "The RFC connection has failed with the following error:".saprfc_error();
                    exit;
                }	*/
            $rfc = connectSAP();

                //We must know if the function really exists
                $fce = saprfc_function_discover($rfc, "Z_FM_M646_GET_THEMATICS");
                if(!$fce){
                        echo "The function module has failed.";
                    echo $rfc;
                    exit;
                }

                $lang = strtoupper($_SESSION['lang']);
                //Pass import parameters
                saprfc_import ($fce,"IP_CODLANG",$lang);

                //Pass table parameters
                saprfc_table_init ($fce,"ET_THEMATIC");

                //Call and execute the function
                $rc = saprfc_call_and_receive ($fce);            
                if ($rfc_rc != SAPRFC_OK){
                        if ($rfc == SAPRFC_EXCEPTION ){
                        echo ("Exception raised: ".saprfc_exception($fce));
                        } else {
                    echo ("Call error: ".saprfc_error($fce));
                        }
                        exit;
                    }

                //Fetch the data from the internal tables
                $data_row = saprfc_table_rows ($fce,"ET_THEMATIC");
            ?>

            <label for="SelectThematic" data-inline="true"><?php echo $texts['thematic'][$_SESSION['lang']] ?>:</label>

            <!-- liste déroulante -->
            <select name="thematic" id="SelectThematic">
                <option value=""><?php echo $texts['all'][$_SESSION['lang']] ?></option>
                <?php

                    for ($i=1; $i<=$data_row; $i++)
                    {
                        $DATARow = saprfc_table_read ($fce,"ET_THEMATIC",$i);
                                //value='" . $DATARow['ID'] ."


                        $option = "<option value='" . $DATARow['ID'] ."'";
                        if($thematicID == $DATARow['ID']){
                            $option .= "selected='selected'";
                        }
                        $option .= ">" . $DATARow['THEMATIC'] . "</option>";

                        echo $option;
                    }
                ?>
            </select>
            <p align="center"><input type="submit" data-inline="true" value="<?php echo $texts['search'][$_SESSION['lang']] ?>" /></p>
        </form>
                <?php

                //Try to connect to SAP using our Login array
                $rfc = connectSAP();
                if(!$rfc){
                        echo "The RFC connection has failed with the following error:".saprfc_error();
                    exit;
                }			

                //We must know if the function really exists
                $fce = saprfc_function_discover($rfc, "Z_FM_M646_GET_EVENTS");
                if(!$fce){
                        echo "The function module has failed.";
                    echo $rfc;
                    exit;
                }    

                //Pass import parameters
                saprfc_import ($fce,"IP_THEMATIC",$thematic);

                //Pass table parameters
                saprfc_table_init ($fce,"GET_EVENTS");

                //Call and execute the function
                $rc = saprfc_call_and_receive ($fce);            
                if ($rfc_rc != SAPRFC_OK){
                        if ($rfc == SAPRFC_EXCEPTION ){
                        echo ("Exception raised: ".saprfc_exception($fce));
                        } else {
                    echo ("Call error: ".saprfc_error($fce));
                        }
                        exit;
                    }

                //Fetch the data from the internal tables
                $data_row = saprfc_table_rows ($fce,"GET_EVENTS");

                if($data_row <= 1){
                    echo '<br /><h3>' . $texts['search_empty'][$_SESSION['lang']] . '</h3>';
                }
                else{

                    /*if(!$_GET['wid']) {  
                            ?>
                            <script language="JavaScript">
                                    window.location="events.php?wid="+document.body.clientWidth;
                            </script>
                            <?php
                    }*/
                    echo '<table width="100%" cellspacing="0"><tr class="titreTableau">';
                    /*if ($_GET['wid']> 600){
                            echo '<td>Tite</td>';
                            echo '<td>Description</td>';
                            echo '<td width="200px">Email</td>';
                            echo '<td width="200px">Site web</td>';
                            echo '<td width="200px">Enregistrement</td>';
                    }
                    else{*/
                            echo '<td width="40%">'. $texts['title'][$_SESSION['lang']] .'</td>';	
                    echo '<td width="60%">'. $texts['dates'][$_SESSION['lang']] .'</td>';
                    //}

                    echo '<tr>';
                
                

                    for ($i=1; $i<=$data_row; $i++)
                    {
                        $DATARow = saprfc_table_read ($fce,"GET_EVENTS",$i);

                        //Simple zebra algorithm
                        $rem = $i % 2;
                        if($rem == 0){
                                echo "<tr bgcolor='#d3d3d3'>";//A8D2FF
                        } else {
                            echo "<tr bgcolor='#FFFFFF'>";
                        } 
                        //Get the correct date format
                        $startdate = substr($DATARow['STARTDATE'], 6, 2) . "." . substr($DATARow['STARTDATE'], 4, 2) . "." . substr($DATARow['STARTDATE'], 0, 4);
                        $enddate = substr($DATARow['ENDDATE'], 6, 2) . "." . substr($DATARow['ENDDATE'], 4, 2) . "." . substr($DATARow['ENDDATE'], 0, 4);
                        $dstartdate = explode(".", $startdate); 
                        $denddate = explode(".", $enddate);

                        $finabFromStart = $dstartdate[2].$dstartdate[1].$dstartdate[0];
                        $finabToEnd = $denddate[2].$denddate[1].$denddate[0]; 
                        if(($finabFrom <= $finabToEnd && $finabTo >= $finabToEnd)){

                            $dateJour = date('d.m.Y');
                            $dJour = explode(".", $dateJour); 
                            $finabJour = $dJour[2].$dJour[1].$dJour[0];

                            echo '<td><a href="./detailEvent.php?event='.$DATARow['ID'].'" rel="external"><img height="17px" src="picture/lens.png"/> '.htmlentities($DATARow['EVENT_TITLE']).'</a></td>';

                            if($startdate != $enddate ){
                                    if($finabJour >= $finabToEnd){
                                        echo "<td><font color=#FF0000>du ". $startdate ." au ". $enddate ."</font></td>";
                                    }
                                    else{
                                        echo "<td>du ". $startdate ." au ". $enddate ."</td>";
                                    }

                            }
                            else{
                                if($finabJour >= $finabToEnd){
                                    echo "<td><font color=#FF0000>le ". $startdate ."</font></td>";
                                }else{
                                    echo "<td>le ". $startdate ."</td>";
                                }
                            }
                        }
                    }
                    echo "</table>";
                }


                //realease the function and close the connection
                saprfc_function_free($fce);
                saprfc_close($rfc);
                ?>
        </div>

    </div><!-- /content -->
    <?php
        include('include/footer.inc.php');  // Nous appelons notre menu
    ?>

</body>
</html>